package cn.zpro.zookeeper.curator;

import cn.zpro.zookeeper.constant.Constants;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;


/**
 * 使用Curator工具对Zk的节点操作
 */
public class CuratorOperatorDemo {

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = Constants.getInstance();
        //createPersistentNodes(curatorFramework);
        createPersistentNodesSequence(curatorFramework);
    }

    /**
     * 添加持久化节点
     */
    static void createPersistentNodes(CuratorFramework curatorFramework) throws Exception {
        String result = curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT)
                .forPath("/curator/codeError","添加持久化节点".getBytes());
        System.out.println(result);
    }

    /**
     * 添加持久化节点-有序
     */
    static void createPersistentNodesSequence(CuratorFramework curatorFramework) throws Exception {
        String result = curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                .forPath("/curator/codeErrorSeq","添加持久化节点-有序".getBytes());
        System.out.println(result);
    }

    /**
     * 添加临时节点
     * EPHEMERAL
     */
    static void createEPHEMERALNodes(CuratorFramework curatorFramework) throws Exception {
        String result = curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL)
                .forPath("/curator/codeError","添加临时节点".getBytes());
        System.out.println(result);
    }

    /**
     * 添加临时节点-有序
     * EPHEMERAL
     */
    static void createEPHEMERALNodesSequence(CuratorFramework curatorFramework) throws Exception {
        String result = curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL)
                .forPath("/curator/codeError","添加临时节点-有序".getBytes());
        System.out.println(result);
    }
}
